package com.devgroupvn.dota;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class ShareSNS extends Activity implements OnClickListener
{
	Context mContext;
	Button btShareFB;
	Button btShareTT;
	Button btPlayvideo;
	TextView tvTitle;
	AdView adView;
	String mTitle;
	String mLink;
	String videolink;
	Entry entryVideo;
	boolean isPlay = false;
	static boolean hasApp;
	public static ProgressDialog progressDialog;

	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mContext = this;
		Share();
		setLink();
	}

	private void Share()
	{
		
		isPlay = false;
		getView();
		setupAdwhirl();
	}

	private void getView()
	{
		setContentView(R.layout.layout_sharesns);
		btShareFB = (Button) findViewById(R.id.btnShareFB);
		btShareTT = (Button) findViewById(R.id.btnShareTT);
		btPlayvideo = (Button) findViewById(R.id.btnPlayvideo);
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		adView = (AdView) findViewById(R.id.adView1);
		btShareFB.setOnClickListener(this);
		btShareTT.setOnClickListener(this);
		btPlayvideo.setOnClickListener(this);
	}

	private void setLink()
	{
		entryVideo = (Entry) Application.getInstance().getData("Entry");
		mTitle = entryVideo.title;
		mLink = entryVideo.mLink;
		videolink = entryVideo.linkvideo;
		tvTitle.setText(mTitle);
		if (videolink.equals(""))
		{
			asynGetVideoLink = new AsyncGetVideoLink(mLink);
			asynGetVideoLink.execute();
		}
	}

	private void setupAdwhirl()
	{
		try
		{
			final Handler handle = new Handler();
			final Runnable run = new Runnable()
			{

				@Override
				public void run()
				{
					AdRequest request = new AdRequest();
					adView.loadAd(request);
					handle.postDelayed(this, 5000);

				}
			};
			handle.postDelayed(run, 5000);
		}
		catch (Exception e)
		{

		}
	}

	@Override
	public void onClick(View v)
	{
		if (v == btShareFB)
		{
			ShareWithFB();
		}
		if (v == btShareTT)
		{
			SharewithTT();
		}
		if (v == btPlayvideo)
		{
			btPlayvideo.setFocusable(false);
			if (!entryVideo.linkvideo.equals(""))
			{
				hasApp = VideoDota2Activity.hasApp;

				if (hasApp)
				{
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setDataAndType(Uri.parse(entryVideo.linkvideo), "video/*");
					mContext.startActivity(intent);
				}
				else
				{
					CheckAppInstall check = new CheckAppInstall(mContext);
					hasApp = check.checkApp(mContext);
					if(!hasApp)
					{
						check.installApp(mContext);
					}
					else
					{
						Intent intent = new Intent(Intent.ACTION_VIEW);
						intent.setDataAndType(Uri.parse(entryVideo.linkvideo), "video/*");
						mContext.startActivity(intent);
					}
				}
			}
			else
			{
				isPlay = true;
				progressDialog = ProgressDialog.show(this, null, "Waiting..", true, false);
			}
			btPlayvideo.setFocusable(true);
		}
	}

	private void ShareWithFB()
	{
		Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
		shareIntent.setType("text/plain");
		shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, mLink);
		PackageManager pm = mContext.getPackageManager();
		List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
		for (final ResolveInfo app : activityList)
		{
			if ((app.activityInfo.name).contains("facebook"))
			{
				final ActivityInfo activity = app.activityInfo;
				final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
				shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
				shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				shareIntent.setComponent(name);
				mContext.startActivity(shareIntent);
				break;
			}
		}
	}

	private void SharewithTT()
	{

		Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
		shareIntent.setType("text/plain");
		shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, mLink);
		PackageManager pm = mContext.getPackageManager();
		List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
		for (final ResolveInfo app : activityList)
		{
			if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
			{
				final ActivityInfo activity = app.activityInfo;
				final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
				shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
				shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				shareIntent.setComponent(name);
				mContext.startActivity(shareIntent);
				break;
			}
		}
	}

	AsyncGetVideoLink asynGetVideoLink;

	public class AsyncGetVideoLink extends AsyncTask<Void, Void, Void>
	{
		String link;
		String path;

		public AsyncGetVideoLink(String _link)
		{
			link = _link;
		}

		@Override
		protected void onPreExecute()
		{
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... arg0)
		{

			try
			{
				path = new WebContent().getPostContent(link);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result)
		{
			super.onPostExecute(result);

			if (progressDialog != null && progressDialog.isShowing())
			{
				progressDialog.dismiss();
			}
			if (path == null) { return; }
			entryVideo.linkvideo = path;

			if (isPlay)
			{

				hasApp = ViewPagerActivity.hasApp;
				if (hasApp)
				{
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setDataAndType(Uri.parse(path), "video/*");
					mContext.startActivity(intent);
				}
				else
				{
					CheckAppInstall check = new CheckAppInstall(mContext);
					check.installApp(mContext);
				}
			}
		}
	}
}
